//
//  LPLeftViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/15.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPLeftViewController.h"
#import "XLSlideMenu.h"
#import "LPLeftHeaderView.h"
#import "LpLeftTableViewCell.h"
#import "HFStretchableTableHeaderView.h"
#import "MyInformationViewController.h"
#import "ZJInfoUpdateViewController.h"
#import "MySettingViewController.h"
#import "LPCostomPressentAnimationSimilarityPushAnimation.h"
#import "ZJNavigationController.h"
#define CELLID @"LpLeftTableViewCell.h"
#define HEADERID @"HEADERVIEW"
#import "LoginViewController.h"
#import "ZJSqlit.h"
#import "YBErWeiMaDownLoadViewController.h"
#import "JPUSHService.h"

@interface LPLeftViewController ()<UITableViewDelegate,UITableViewDataSource,LPLeftHeaderDelegate>
//{
//    MySettingViewController *_mySettingViewController;
//}
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  标题  */
@property (strong, nonatomic) NSArray  *dataArray;
/**    */
@property (strong, nonatomic) HFStretchableTableHeaderView  *hfStretch;

@property (nonatomic , strong) UIButton * logoutButton;
@end

@implementation LPLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor themeGrayColor];
    self.navigView.hidden = YES;
    [self getDataSource];
    [self layoutUI];
}

#pragma mark - 数据
- (void)getDataSource{
    NSArray *array1 = @[@{@"image": @"mine_update", @"title": @"信息更新"},
                       @{@"image": @"mine_exchange", @"title": @"同步数据"},
                       @{@"image": @"mine_set", @"title": @"设置"},
                       @{@"image": @"mine_saoma", @"title": @"扫码下载"}];
    
    self.dataArray = @[array1];
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.logoutButton];
    LPLeftHeaderView *header = [[LPLeftHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.xl_sldeMenu.menuWidth, 200 * [UIScreen zj_screenHScale])];
    header.delegate = self;
    [self.hfStretch stretchHeaderForTableView:self.tableView withView:header imageView:header.backImageView share:header.name headerImaheView:header.headerImageView erweima:header.erweimaLabel];
    
    
//    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.xl_sldeMenu.menuWidth, 10 * [UIScreen zj_screenHScale])];
//    footer.backgroundColor = [UIColor themeGrayColor];
//    self.tableView.tableFooterView = footer;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


#pragma mark - 私有方法


#pragma mark - LPLeftHeaderDelegate
- (void)LPLeftHeadertap_erweima:(BOOL)tap{

//    [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];

}

- (void)LPLeftHeadertap_header:(BOOL)tap{
    MyInformationViewController * infoVC = [[MyInformationViewController alloc]init];
    [[ToolClass getMainNavigationController] pushViewController:infoVC animated:YES];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.hfStretch scrollViewDidScroll:scrollView];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ((NSArray *)self.dataArray[section]).count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LpLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
        cell.data = ((NSArray *)self.dataArray[indexPath.section])[indexPath.row];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HEADERID];
//    view.backgroundColor = [UIColor zj_colorWithHexString:@"ebebeb"];
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000001;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ZJInfoUpdateViewController *vc = [ZJInfoUpdateViewController new];
        [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 1){
        [self ybSynchronousCacheData];
    }
    else if (indexPath.row == 2){
        MySettingViewController *vc = [MySettingViewController new];
        [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 3){
        YBErWeiMaDownLoadViewController *vc = [YBErWeiMaDownLoadViewController new];
        [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    }
    
}


-(void)ybSynchronousCacheData{
    [YJProgressHUD showMessage:@"同步中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    [YBRequestManager request_loginRequestForCacheWithSuccessBlock:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            ZJSqlit * mySqlite = [[ZJSqlit alloc]init];
            [mySqlite createTableWithName:@"ORG_OFTEN_DEPTS"];
            [mySqlite putObject:object[@"ORG_OFTEN_DEPTS"] withId:YBUSERDATAKEY intoTable:@"ORG_OFTEN_DEPTS"];
            
            [mySqlite createTableWithName:@"ORG_OFTEN_USERS"];
            [mySqlite putObject:object[@"ORG_OFTEN_USERS"] withId:YBUSERDATAKEY intoTable:@"ORG_OFTEN_USERS"];
            
            [mySqlite createTableWithName:@"ORG_DEPTS"];
            [mySqlite putObject:object[@"ORG_DEPTS"] withId:YBUSERDATAKEY intoTable:@"ORG_DEPTS"];
            
            [mySqlite createTableWithName:@"ORG_USERS"];
            [mySqlite putObject:object[@"ORG_USERS"] withId:YBUSERDATAKEY intoTable:@"ORG_USERS"];
            [YJProgressHUD showSuccess:@"同步成功" inview:[UIApplication sharedApplication].keyWindow];
        }
        else{
            [YJProgressHUD showSuccess:@"同步失败" inview:[UIApplication sharedApplication].keyWindow];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
    }];
}


-(void)option_logoutButton:(UIButton *)sender{
    //退出登录
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"是否退出登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        /**  这是退出登录  */
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"isLogin"];
        [JPUSHService setTags:[NSSet set] alias:@"" callbackSelector:nil object:nil];
        //xmpp退出登录
        //         [[DRRRManager sharedManager] signout];
        //停止接受推送
        //        [weakSelf jpush];
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"YBHomeRequestTime"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:USER_INFO];
        [ZJUserInfoModel updateUserInfoFormNSUserDefaults];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
        UIWindow * window =  [[UIApplication sharedApplication].delegate window];
        window.rootViewController = nav;
        
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake([UIScreen zj_screenWith] - self.xl_sldeMenu.menuWidth, 0, self.xl_sldeMenu.menuWidth, [UIScreen zj_screenHScale] * 420) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor themeGrayColor];
        [_tableView adaptiveIOS11];
//        _tableView.tableFooterView = [[TenGrayShadowView  alloc] initWithFrame:CGRectMake(0, 0, self.xl_sldeMenu.menuWidth, 10)];
        [_tableView registerClass:[LpLeftTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}

- (HFStretchableTableHeaderView *)hfStretch{
    if (!_hfStretch) {
        _hfStretch = [[HFStretchableTableHeaderView alloc] init];;
    }
    return _hfStretch;
}


-(UIButton *)logoutButton
{
    if (!_logoutButton) {
        _logoutButton = [ToolClass createButtonWithFrame:CGRectMake([UIScreen zj_screenWith] - self.xl_sldeMenu.menuWidth + [UIScreen zj_screenWScale] * 20, [UIScreen zj_screenHScale] * 500, self.xl_sldeMenu.menuWidth - [UIScreen zj_screenWScale] * 40, [UIScreen zj_screenHScale] * 45) text:@"退出登录" textColor:[UIColor whiteColor] imageName:@"" font:16];
        _logoutButton.backgroundColor = [UIColor themeColor];
        [_logoutButton addTarget:self action:@selector(option_logoutButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutButton;
}


@end
